#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/url_dispatcher.h>
#include <cppcms/url_mapper.h>
#include <cppcms/http_request.h>
#include <iostream>
#include <stdlib.h>
#include <nlohmann/json.hpp>
#include <cstring>
#include "ip.cpp"

using namespace std;
using json=nlohmann::json;

class web : public cppcms::application {
public:
    web(cppcms::service &srv) :cppcms::application(srv)
    {
        // dispatcher().assign("/number/(\\d+)",&web::number,this,1);
        //    mapper().assign("number","/number/{1}");

  
        dispatcher().assign("/index",&web::index,this);
        mapper().assign("/index");

        dispatcher().assign("/list",&web::list,this);
        mapper().assign("/list");

        dispatcher().assign("/limit",&web::limit,this);
        mapper().assign("/limit");

        dispatcher().assign("/page",&web::page,this);
        mapper().assign("/page");

        dispatcher().assign("/search",&web::search,this);
        mapper().assign("/search");

        dispatcher().assign("/searchpage",&web::searchpage,this);
        mapper().assign("/searchpage");

        dispatcher().assign("/searchlimit",&web::searchlimit,this);
        mapper().assign("/searchlimit");

        mapper().root("/web");
    }
    
    
    virtual void index();
    virtual void list();
    virtual void limit();
    virtual void page();
    virtual void search();
    virtual void searchpage();
    virtual void searchlimit();
};

void web::index()
{
    string ip= request().get("ip");

    string ipinfo=ipaddr(ip);



    response().set_header("Access-Control-Allow-Origin","*");
    response().set_header("Access-Control-Allow-Methods","*");
    response().set_header("Access-Control-Allow-Headers","*");
    response().set_header("Access-Control-Allow-Credentials", "true");
    response().content_type("application/json; charset=utf-8");
    response().out() << ipinfo;
}

void web::list()
{
    
    string s=listindex();


    response().set_header("Access-Control-Allow-Origin","*");
    response().set_header("Access-Control-Allow-Methods","*");
    response().set_header("Access-Control-Allow-Headers","*");
    response().set_header("Access-Control-Allow-Credentials", "true");
    response().content_type("application/json; charset=utf-8");
    response().out() << s;
}

void web::page()
{
    
    string s=gpage();


    response().set_header("Access-Control-Allow-Origin","*");
    response().set_header("Access-Control-Allow-Methods","*");
    response().set_header("Access-Control-Allow-Headers","*");
    response().set_header("Access-Control-Allow-Credentials", "true");
    response().content_type("application/json; charset=utf-8");
    response().out() << s;
}

void web::search()
{
    string key=request().get("key");
    string s=gsearch(key);


    response().set_header("Access-Control-Allow-Origin","*");
    response().set_header("Access-Control-Allow-Methods","*");
    response().set_header("Access-Control-Allow-Headers","*");
    response().set_header("Access-Control-Allow-Credentials", "true");
    response().content_type("application/json; charset=utf-8");
    response().out() << s;
}

void web::searchpage()
{
    string key=request().get("key");
    string s=gsearchpage(key);
 


    response().set_header("Access-Control-Allow-Origin","*");
    response().set_header("Access-Control-Allow-Methods","*");
    response().set_header("Access-Control-Allow-Headers","*");
    response().set_header("Access-Control-Allow-Credentials", "true");
    response().content_type("application/json; charset=utf-8");
    response().out() << s;
}

void web::searchlimit()
{
    string key=request().get("key");
    string s;
    string ss=request().get("page");
    if(!ss.empty())
    {
        int page=std::stoi(ss);

        if (page==0 || !page) s=gsearchlimit(page,key);
        else{
            page=(page-1);
            s=gsearchlimit(page,key);
        }
    }else{
        int page=0;
        s=gsearchlimit(page,key);
    }

    response().set_header("Access-Control-Allow-Origin","*");
    response().set_header("Access-Control-Allow-Methods","*");
    response().set_header("Access-Control-Allow-Headers","*");
    response().set_header("Access-Control-Allow-Credentials", "true");
    response().content_type("application/json; charset=utf-8");
    response().out() << s;
}

void web::limit()
{
    string s;
    string ss=request().get("page");
    if(!ss.empty())
    {
        int page=std::stoi(ss);

        if (page==0 || !page) s=glimit(page);
        else{
            page=(page-1);
            s=glimit(page);
        }
    }else{
        int page=0;
        s=glimit(page);
    }


    response().set_header("Access-Control-Allow-Origin","*");
    response().set_header("Access-Control-Allow-Methods","*");
    response().set_header("Access-Control-Allow-Headers","*");
    response().set_header("Access-Control-Allow-Credentials", "true");
    response().content_type("application/json; charset=utf-8");
    response().out() << s;
}

int main(int argc,char ** argv)
{
    try {
        cppcms::service srv(argc,argv);

        srv.applications_pool().mount(
          cppcms::applications_factory<web>()
        );
        srv.run();
    }
    catch(std::exception const &e) {
        std::cerr << e.what() << std::endl;
    }
}
